<template>
  <ContentWrap>
    <template #header>
      <div class="card-header">
        <span class="card-title">访问数字资源</span>
      </div>
    </template>
    <div class="card-content">
      <div class="chart" ref="chartRef"></div>
    </div>
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as echarts from 'echarts'
import { ReportApi } from '@/api/dr/report'
const chartRef = shallowRef()
const myChart = shallowRef()
const dataList = ref<any>([
  // { value: 1048, name: '陶器' },
  // { value: 735, name: '瓷器' },
  // { value: 580, name: '青铜器' },
  // { value: 484, name: '书画' },
  // { value: 484, name: '绘画' },
  // { value: 484, name: '玉石器' },
  // { value: 484, name: '石刻' },
  // { value: 484, name: '杂项' },
])
const initChart = () => {
  myChart.value = echarts.init(chartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      left: 'right',
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    color: ['#FF8C46', '#948064', '#C3C5AC', '#F5EDD9', '#D8C9A7', '#AB9266'],
    yAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: dataList.value.map((v:any) => v.name),
      }
    ],
    xAxis: {
      type: 'value',
      axisTick: { show: false },
    },
    series: [
      {
        name: '次数',
        type: 'bar',
        barWidth: 30,
        emphasis: {
          focus: 'series'
        },
        data: dataList.value.map((v:any) => v.value),
      },
    ]
  }
  myChart.value.setOption(option)
  window.addEventListener('resize', () => {
    myChart.value.resize()
  })
}
const getData = () => {
  ReportApi.getVisitCount().then((res) => {
    // console.log('getVisitCount', res)
    const list = res.sourceTypeDataRespVOS || []
    dataList.value = list.map((item: any) => ({
      value: item.dataNum,
      name: item.dataName
    }))
    initChart() 
  })
}
onMounted(() => {
  getData()
})
</script>

<style lang="scss" scoped>
.card-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .card-title {
    font-weight: bold;
    line-height: 32px;
  }
  .more {
    font-size: 14px;
  }
  :deep(.el-tabs__header) {
    margin: 0;
  }
}
.card-content {
  padding: 0 10px;
}
.chart {
  width: 100%;
  height: 300px;
}
:deep(.el-card__header) {
  padding: 10px;
}
</style>
